package com.ziven.common.widget.value;

import android.view.View;

import androidx.annotation.NonNull;

import com.ziven.easygo.util.Carry;
import com.ziven.easygo.util.EasyUtils;
import com.ziven.easygo.util.ViewHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Ziven
 */
public abstract class MixValue extends Value {

    private final List<Value> valueList;

    public MixValue(@NonNull Value... values) {
        valueList = new ArrayList<>(values.length);
        add(values);
    }

    public MixValue add(@NonNull Value... values) {
        EasyUtils.forEach(values, value -> valueList.add(value));
        return this;
    }

    public MixValue add(@NonNull List<Value> values) {
        valueList.addAll(values);
        return this;
    }

    public MixValue replace(@NonNull List<Value> values) {
        valueList.clear();
        valueList.addAll(values);
        return this;
    }

    public MixValue remove(@NonNull Value value) {
        if(!valueList.isEmpty()) {
            valueList.remove(value);
        }
        return this;
    }

    public MixValue forEach(@NonNull Carry<Value> carry) {
        EasyUtils.forEach(valueList, carry);
        return this;
    }

    public List<Value> obtain() {
        return new ArrayList<>(valueList);
    }

    @Override
    protected void takeEffect(@NonNull ViewHelper<? extends View> helper) {
        if(!valueList.isEmpty()) {
            takeEffect(helper, valueList.toArray(new Value[0]));
        }
    }

    /**
     * Take Effect
     * @param helper ViewHelper
     * @param values Value Array
     */
    protected abstract void takeEffect(@NonNull ViewHelper<? extends View> helper, @NonNull Value[] values);

    @Override
    public Value copy(@NonNull Value v) {
        v.myself(MixValue.class, my -> my.replace(valueList));
        return super.copy(v);
    }

    @NonNull
    @Override
    public String toString() {
        return "MixValue{" +
                "valueList=" + valueList +
                '}';
    }
}
